﻿using ClassLibrary;
using ClassLibrary.Service;
using Forms;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace MainForm
{
    public partial class MainForm : UIForm
    {
        User user = new User();
        InteractionService interactionService = new InteractionService();
        UserService userService = new UserService();
        public static Color userColor = Color.FromArgb(80, 160, 255);
        public MainForm(User user)
        {
            InitializeComponent();
            // 订阅全局变量的改变事件
            FlashVariables.FlashVariableChanged += FlashVariableChangedHandler;
            SendDemands.Click += SendDemands_Click;
            EnterSupermarket.Click += EnterSupermarket_Click;
            ModifyBasicInformation.Click += ModifyBasicInformation_Click;
            AboutApplication.Click += AboutApplication_Click;
            MyOrder.Click += MyOrder_Click;
            MySuper.Click += MySuper_Click;
            ThemeColor.Click += ThemeColor_Click;
            this.user = user;
            InitPerInfomation();//初始化个人信息
            ListMyMessage(user);//初始化我的消息
            userColor = System.Drawing.ColorTranslator.FromHtml(user.UserColor);
            UIStyles.InitColorful(userColor, Color.White);
        }

        private void ThemeColor_Click(object? sender, EventArgs e)
        {
            ThemeColor themeColor = new ThemeColor(user);
            themeColor.ShowDialog();
            userColor = System.Drawing.ColorTranslator.FromHtml(user.UserColor);
            UIStyles.InitColorful(userColor, Color.White);
        }

        private void FlashVariableChangedHandler(object sender, EventArgs e)
        {
            // 在这里更新界面
            ListMyMessage(user);//初始化我的消息
        }

        // 最好在窗体关闭时移除事件订阅，避免潜在的内存泄漏
        private void YourForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            FlashVariables.FlashVariableChanged -= FlashVariableChangedHandler;
        }
        private void MySuper_Click(object? sender, EventArgs e)
        {
            MySupermarket mySupermarket = new MySupermarket(user);
            mySupermarket.ShowDialog();
        }

        private void EnterSupermarket_Click(object sender, EventArgs e)
        {
            Supermarket supermarket = new Supermarket(user);
            supermarket.ShowDialog();
        }

        private void SendDemands_Click(object sender, EventArgs e)
        {
            //测试时暂时注释
            DemandForm demandForm = new DemandForm(user);
            demandForm.ShowDialog();
        }

        private void Button1MenuStrip_Opening(object sender, CancelEventArgs e)
        {

        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            // 获取按钮的位置
            Point buttonLocation = Setting.PointToScreen(Point.Empty);

            // 计算菜单的显示位置
            int x = buttonLocation.X;
            int y = buttonLocation.Y - Button1MenuStrip.Height;

            // 在按钮上方显示菜单
            Button1MenuStrip.Show(x, y);
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {

            // 获取按钮的位置
            Point buttonLocation = Function.PointToScreen(Point.Empty);

            // 计算菜单的显示位置
            int x = buttonLocation.X;
            int y = buttonLocation.Y - Button2MenuStrip.Height;

            // 在按钮上方显示菜单
            Button2MenuStrip.Show(x, y);
        }
        private void InitPerInfomation()//初始化个人信息
        {
            UserName.Text = user.UserName;
            StudentID.Text = user.UserID;
            Telephone.Text = user.UserTele;
            ID.Text = user.UserIdentityCard;
            Address.Text = user.UserAddress;
            pictureBox1.Image = ImageService.BytesToImage(ImageService.getProfileByID(user.UserID));
            UIStyles.InitColorful(userColor, Color.White);
        }

        private void Profile_Click(object sender, EventArgs e)//点击头像
        {
            ProfileForm profileForm = new ProfileForm(user, pictureBox1.Image, user);
            profileForm.ShowDialog();
        }

        private void ListMyMessage(User Receiver)//初始化列表化我的消息
        {
            panel.Controls.Clear();
            List<User> Senders = new List<User>();
            Senders = interactionService.GetSenderIDByReceiver(user);//根据该用户，返回所有给他发过消息的用户
            panel.AutoScroll = true; // 启用自动滚动条
            int boxHeight = 120; //消息框高120
            int i = 0;
            foreach (User Sender in Senders)//对于每个发送者有一个消息框
            {
                MyMessagebox box = new MyMessagebox(Sender, Receiver);
                box.Top = i * boxHeight; // 设置用户控件的位置
                i++;
                panel.Controls.Add(box); // 将用户控件添加到 Panel 中
            }
        }

        private void ModifyBasicInformation_Click(object sender, EventArgs e)
        {
            ModifyBasicInformationForm modifyBasicInformation = new ModifyBasicInformationForm(user, pictureBox1.Image);
            modifyBasicInformation.ShowDialog();
            user = userService.GetUserByID(user.UserID)[0];
            InitPerInfomation();
        }

        private void Introduction_Click(object sender, EventArgs e)
        {
            // 获取按钮的位置
            Point buttonLocation = Introduction.PointToScreen(Point.Empty);

            // 计算菜单的显示位置
            int x = buttonLocation.X;
            int y = buttonLocation.Y - Button3MenuStrip.Height;

            // 在按钮上方显示菜单
            Button3MenuStrip.Show(x, y);
        }

        private void AboutApplication_Click(object sender, EventArgs e)
        {
            Introduce introduce = new Introduce();
            introduce.Show();

        }
        private void MyOrder_Click(object sender, EventArgs e)
        {
            MyOrder myOrder = new MyOrder(user);
            myOrder.Show();
        }
        private void Button3MenuStrip_Opening(object sender, CancelEventArgs e)
        {

        }

        private void Button1MenuStrip_Opening_1(object sender, CancelEventArgs e)
        {

        }
    }
}
